from bge import logic
from bge import events
import random


def Player():
	
	cont = logic.getCurrentController()
	player = cont.owner
	scene = logic.getCurrentScene()
	touch = cont.sensors['touch']		
	move = cont.actuators['move']
	track = cont.actuators['track']
	colStacja = cont.sensors['colStacja']
	
	
	def Init():
		
		global lastPosition
		if not 'init' in player:
			player['init'] = 1
		
		

	def getTarget():
		#Szkola, Stacja, Rozrywka, Praca
		objects = ["Szkola", "Rozrywka", "Praca"]

		needed = getNeeded()

		if needed == 'power':
			player['dest'] = 'Stacja'
		elif needed == 'money':
			player['dest'] = 'Praca'
		elif needed == 'knowledge':
			player['dest'] = 'Szkola'
		elif needed == 'happiness':
			player['dest'] = 'Rozrywka'
		else:
			return player['dest']

		return player['dest']
   
	def movePlayer():
		player['power'] -= 1
		

	def getNeeded():
		#print("Czy potrzeba?")
		if player['needed'] != "":
			#print ("Brak potrzeb")
			return ""

		print("Pobierz potrzebe")

		power = player['power']
		knowledge = player['knowledge']
		happiness = player['happiness']
		money = player['money']

		neededList = ['power', 'knowledge', 'happiness', 'money']
		
		lastNeeded = player['needed']

		if power < 500 :
			player['needed'] = 'power'
		elif money < 300:
			player['needed'] = 'money'
		elif happiness < 300 :
			player['needed'] = 'happiness'
		else:
			player['needed'] = 'knowledge'


		return player['needed']

	def colisionAction():
		player['needed'] = ""
		print("CZYSZCZENIE POTRZEBY")
		track.object = getTarget()
		print("Pobieranie celu: ", track.object)
		


	def Update(): 
		
		stacjaObj = scene.objects["Stacja"]
		
		hit = touch.positive
		
		
		#path = ['Node', 'Node.001']
		speed = [0,4,0]
		
		point = player['point']
		
		#if player.getDistanceTo(track.object) <= 1:			
			
		#Only start
		if track.object is None:
			track.object = getTarget()
			print('START')

		

		if colStacja.positive:
			player['power'] = 1000
			player['money'] -= 300
			player['happiness'] -= 50
			colisionAction()

		if hit:
			if touch.hitObject.name != 'Plane':
				print("KOLIZJA:", touch.hitObject.name)
				speed[1] = 0
			
				#if touch.hitObject.name == 'Stacja' and player['money'] > 250:
					
				if touch.hitObject.name == 'Praca':
					player['money'] += 250
					player['power'] -= 5
					player['happiness'] -= 100
				elif touch.hitObject.name == 'Szkola':
					player['knowledge'] += 100
					player['money'] -= 100
					player['happiness'] -= 5
				elif touch.hitObject.name == 'Rozrywka':
					player['happiness'] += 300
					player['money'] -= 100

				
		
		#print(player.getDistanceTo(track.object))
		
		if player.getDistanceTo(track.object) <= 2.5:			
			track.object = getTarget()
			print("Pobieranie celu: ", track.object)

		if player['power'] > 0:			     
			cont.activate(track)
			#player['point'] = point
			#player['dir'] = dir			
			movePlayer()
		else:
			speed[1] = 0


		#------------- MOVE--------------------
		move.linV = speed
		move.useLocalLinV = 1
		cont.activate(move) 	
	
	Init()	
	Update()
	
